package io.dshubs.archives.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.dshubs.archives.dao.ArchiveMyFavoriteMapper;
import io.dshubs.archives.entity.ArchiveBaseCategory;
import io.dshubs.archives.entity.ArchiveDestroy;
import io.dshubs.archives.entity.ArchiveMyFavorite;
import io.dshubs.archives.service.ArchiveMyFavoriteService;
import io.dshubs.commons.mybatis.enums.DelFlagEnum;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.security.user.UserDetail;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.enums.SuperAdminEnum;
import io.dshubs.commons.tools.page.PageData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案鉴定表 服务实现类
 * </p>
 *
 * @author tan
 * @since 2021-08-27
 */
@Service
public class ArchiveMyFavoriteServiceImpl extends BaseServiceImpl<ArchiveMyFavoriteMapper, ArchiveMyFavorite> implements ArchiveMyFavoriteService {

    @Autowired
    private ArchiveMyFavoriteMapper archiveMyFavoriteMapper;


    @Override
    public PageData<ArchiveMyFavorite> selectMyFavoritePage(Map<String, Object> params) {
        UserDetail user = SecurityUser.getUser();
        params.put("userId",user.getId());
        IPage<ArchiveMyFavorite> page = getPage(params, Constant.CREATE_DATE, false);
        long startLine = page.getCurrent();
        long pageSize = page.getSize();
        List<ArchiveMyFavorite> list = archiveMyFavoriteMapper.selectMyFavoriteList(params, startLine, pageSize);
        int count = archiveMyFavoriteMapper.selectMyFavoriteListCount(params);
        PageData<ArchiveMyFavorite> pageResult = new PageData<ArchiveMyFavorite>();
        pageResult.setList(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    private QueryWrapper<ArchiveMyFavorite> getWrapper(Map<String, Object> params){
        String fileNumber = (String)params.get("fileNumber");
        QueryWrapper<ArchiveMyFavorite> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(fileNumber), "file_number", fileNumber);
        UserDetail user = SecurityUser.getUser();
        wrapper.eq("user_id",user.getId());
        return wrapper;
    }

    @Override
    public int insertMyFavorite(ArchiveMyFavorite archiveMyFavorite) {
        UserDetail user = SecurityUser.getUser();
        archiveMyFavorite.setUserId(user.getId());
        archiveMyFavorite.setCreator(user.getId());
        archiveMyFavorite.setUpdater(user.getId());
        return archiveMyFavoriteMapper.insertMyFavorite(archiveMyFavorite);
    }

    @Override
    public int deleteMyFavorite(List<Long> ids) {
       ids.forEach(i->{
           ArchiveMyFavorite archiveMyFavorite = new ArchiveMyFavorite();
           UserDetail user = SecurityUser.getUser();
           archiveMyFavorite.setUserId(user.getId());
           archiveMyFavorite.setId(i);
            archiveMyFavoriteMapper.deleteMyFavorite(archiveMyFavorite);
       });
        return 0;
    }

    @Override
    public int cancleMyFavorite(Long archiveId) {
        ArchiveMyFavorite archiveMyFavorite = new ArchiveMyFavorite();
        UserDetail user = SecurityUser.getUser();
        archiveMyFavorite.setUserId(user.getId());
        archiveMyFavorite.setArchiveId(archiveId);
        return archiveMyFavoriteMapper.cancleMyFavorite(archiveMyFavorite);
    }
}
